package edu.ubbcluj.turisztikai.activity;

import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.StringTokenizer;

import turistic.GUI.graphic.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;

public class SettingsActivity extends Activity {
	private static final long MIN_LOCATION_UPDATE_TIME = 30000;
	private static final float MIN_LOCATION_UPDATE_DISTANCE = 30;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);
		Intent sender = getIntent();
		final String x = sender.getExtras().getString("Coordinates");
		
		StringTokenizer st = new StringTokenizer(x, " ");
		st.nextToken();
		st.nextToken();
		final double lat = Double.parseDouble(st.nextToken());
		final double lng = Double.parseDouble(st.nextToken());
		
		LManager();

		Button editB = (Button) findViewById(R.id.editB);
		editB.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(new Intent(SettingsActivity.this,
						EditActivity.class));
			}
		});

		Button viewB = (Button) findViewById(R.id.viewB);
		viewB.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(new Intent(SettingsActivity.this,
						RouteViewActivity.class));
			}
		});

		Button beszamolo = (Button) findViewById(R.id.beszamolo);
		beszamolo.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(new Intent(SettingsActivity.this,
						BeszamoloActivity.class));
			}
		});

		Button backB = (Button) findViewById(R.id.backB);
		backB.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(new Intent(SettingsActivity.this,
						ParamActivity.class));
			}
		});
		Button vegezd = (Button) findViewById(R.id.vegezd);
		vegezd.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				RadioButton r1 = (RadioButton) findViewById(R.id.radio0);
				if (r1.isChecked()) {
					
					
					
					Intent intent = new Intent(SettingsActivity.this,
							GoogleMapsActivity.class);
					intent.putExtra("Coordinates", x);

					startActivity(intent);
				} else {
					Context context;
					
					Geocoder gcd = new Geocoder(SettingsActivity.this, Locale.getDefault());
					List<Address> addresses;
					try {
						addresses = gcd.getFromLocation(lat, lng, 1);
						String adr = addresses.get(0).getFeatureName();
						if (adr == null) 
							messageBox("Hibas Koordinatak!");
						else
						    startNavigation(adr);
						
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}

			}
		});
	}

	public void LManager() {
		LocationManager locator = (LocationManager) getSystemService(this.LOCATION_SERVICE);
		LocationListener mLocationListener = new LocationListener() {

			public void onLocationChanged(Location location) {
			}

			public void onProviderDisabled(String provider) {
			}

			public void onProviderEnabled(String provider) {
			}

			public void onStatusChanged(String provider, int status,
					Bundle extras) {
			}
		};

		Criteria criteria = new Criteria();

		criteria.setAccuracy(Criteria.ACCURACY_FINE); // More accurate, GPS
														// fix.
		// You can specify the time and distance between location updates.
		// Both are useful for reducing power requirements.
		locator.requestLocationUpdates(locator.GPS_PROVIDER,
				MIN_LOCATION_UPDATE_TIME, MIN_LOCATION_UPDATE_DISTANCE,
				mLocationListener, getMainLooper());
	}

	public void startNavigation(String address) {
		address = address.replace('#', ' ');
		Log.i("NavStarter", "Starting navigation to [" + address + "]");
		// if (address != null) { // History saving
		// db.execSQL("INSERT OR IGNORE INTO " + dbTable + " VALUES ('" +
		// address
		// + "');");

		Intent i = new Intent();
		i.setAction(Intent.ACTION_VIEW);
		i.setData(Uri.parse("http://maps.google.com/maps?myl=saddr&daddr="
				+ address + "&dirflg=d&nav=1"));
		i.addFlags(0x10800000);
		i.setClassName("com.google.android.apps.m4ps",
				"com.google.android.maps.driveabout.app.NavigationActivity");

		try {
			startActivity(i);
			finish();
		} catch (ActivityNotFoundException e) {
			i.setClassName("com.google.android.apps.maps",
					"com.google.android.maps.driveabout.app.NavigationActivity");
			try {
				startActivity(i);
				finish();
			} catch (ActivityNotFoundException e2) {
				// TODO Print error message because not the right maps.apk is
				// installed
				Log.e("NavStarter",
						e.getLocalizedMessage() + "\n"
								+ e2.getLocalizedMessage());
				// new android.app.AlertDialog.Builder(this).setTitle(
				// R.string.intent_error_title).setMessage(
				// R.string.intent_error_body).setIcon(
				// android.R.drawable.ic_dialog_alert).create().show();
			}
		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			startActivity(new Intent(SettingsActivity.this,
					ParamActivity.class));
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}

	private void messageBox(String mess) {
		final AlertDialog alertDialog;
		alertDialog = new AlertDialog.Builder(this).create();

		alertDialog.setTitle("Uzenet");
		alertDialog.setMessage(mess);

		alertDialog.show();

	}
}
